import config from '@/common/config.js'

export const getHeaderSysInfo=()=>{
	const {clientId,version} = config
	let uaSTr = uni.getStorageSync('ua_str')
	
	if(uaSTr){
		return uaSTr
	}
	const res = uni.getSystemInfoSync() //'Sys/iPhone OS/11.20 Model/iPhone8,1 Ver/1.0.2 PKG/com.shanai.shanai SCR/375X667 Ca/0'
	uaSTr += 'Sys/' + res.brand + ' OS/'
	let sys = res.system.split(" ")
	uaSTr += sys[1] + ' Model/'
	if (res.brand === 'iPhone') {
		var model = res.model.split("<")
		model = model[1].substring(0, (model[1].length) - 1)//截掉 >
	} else {
		model = res.model
	}
	uaSTr += model + ' Ver/'//此处需要修改
	uaSTr += version + ' PKG/'
	uaSTr += clientId + ' SCR/'
	uaSTr += res.screenWidth + 'X' + res.screenHeight + ' Ca/'
	uaSTr += clientId
	return uaSTr
}

export const getAddressText=(address)=>{
	if(!address||!address.province_name){return ''}
	const temp=[...new Set([address.province_name,address.city_name,address.area_name])].join(' ')
	return temp+ ' '+address.address_detail
}

/**
 * @param {object} target
 * @param {object} resource
 * @returns {Object}
 */
export function mergeObj(target, resource) {
  if (!target || typeof target !== 'object' || !resource || typeof resource !== 'object') {
    return target
  }
  const _target = JSON.parse(JSON.stringify(target))
  Object.keys(_target).forEach(key => {
    if (resource.hasOwnProperty(key)) {
      const value = resource[key]
      if (typeof value === 'object') {
        _target[key] = JSON.parse(JSON.stringify(value))
      } else {
        _target[key] = value
      }
    }
  })
  return _target
}

export function authorize(scope,cb,text='请在设置中允许该授权'){
	uni.authorize({
		scope:'scope.'+scope,
		success:()=>{
			cb()
		},
		fail(res){
			console.log(res)
			uni.showToast({
				icon:'none',
				title:text
			})
			uni.openSetting()
		}
	})
}